using SmartLink.RedisRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure.Cache
{
    /// <summary>
    /// 角色用户关联缓存
    /// </summary>
    public static class RoleToUsersCache
    {
        private const string _redisKeyPre = RedisKeyPre.RoleToUsers;

        public static bool SetUser(int roleId, int userId)
        {
            return RedisManager.GetInstance().SetAsync(_redisKeyPre + roleId, userId.ToString());
        }

        public static bool SetUsers(int roleId, int[] userIds)
        {
            return RedisManager.GetInstance().SetAsync(_redisKeyPre + roleId, Array.ConvertAll(userIds, id => id.ToString()));
        }

        public static List<int> GetUsers(int roleId)
        {
            return RedisManager.GetInstance().SetMembers(_redisKeyPre + roleId).Select(a => int.Parse(a)).ToList();
        }

        public static bool RemoveUser(int roleId, int userId)
        {
            return RedisManager.GetInstance().SetRemoveAsync(_redisKeyPre + roleId, userId.ToString());
        }
    }
}
